<?php
class helper_userbase_reg
{
	/**
	 * 
	 * @return 失败时返回的错误提示内容
	 * @var string
	 */
	public $errmsg = '';
	
	protected $userinfo = null;
	
	/**
	 * 
	 * @return 注册成功后可提取用户对象
	 * @var Userbase
	 */
	public $userbase = null;
	/**
	 * 
	 * @return 绑定到用户充值的点卡产品上
	 * @var Product 
	 */
	protected $product = null;
	/**
	 * 
	 * Enter description here ...
	 * @var int 产品ID
	 */
	protected $productId = 22;
	/**
	 * 
	 * @return 注册成功后回显用
	 * @var Productcard
	 */
	public $productcard = null;
	
	/**
	 * 
	 *2012-1-4-下午04:47:41 by 460932465
	 * @return 返回USERID是否存在数据库
	 * @param string|用户ID $userid
	 */
	static function validate_userid_reg($userid)
	{
		$user = Userbase::find('userid = ?',$userid)->getOne();
		if ($user->id())
		{
			return true;
		}else
		{
			return false;
		}
		
	}
	/**
	 *
	 *2011-12-30-下午04:16:07 by 460932465
	 * @return 返回手机号码是否存在数据库 存在返回true 反之false ...
	 * @param string|手机号码 $mobile
	 */
	static function validate_mobile_reg($mobile)
	{
		$user = Userbase::find('userphone = ?',$mobile)->getOne();
		if ($user->id())
		{
			return true;
		}else
		{
			return false;
		}
	}
	/**
	 * 
	 *2011-12-30-下午04:27:37 by 460932465
	 * @return 注册前执行，返回true继续注册,false停止...
	 */
	protected function _befor_reg()
	{
		//先验证是否存在用户
		if ( ! self::validate_mobile_reg($this->userinfo['userphone']))
		{
		$this->product = Product::find('id = ?',$this->productId)->getOne();
		if ($this->product->id())
		{
			if (count($this->product->card)>0)
			{
				$productCard = Productcard::find('p_code = ? and status = 0',$this->product->p_code)->getOne();
				if ($productCard->id())
				{
					
					//开始充值
					$pay = new helper_interface_pay($this->userinfo['userphone']);
					//$pay->set_usermobile($this->userinfo['userphone']);
					$payFlag = $pay->pay_card($productCard);
						if($payFlag === true)
						{
							//绑定用户的账号密码为第一次的点卡和密码
							$this->userinfo['userid'] = $productCard->code;
							$this->userinfo['userpass'] = md5($productCard->pass);
							$this->productcard = $productCard;
							return true; //充值成功 返回真
							//是否赠送注册用户积分
							//$this->userinfo['userpoint']
						}else
						{
							//回现接口提示错误
							$this->errmsg = $pay->errmsg;
							return false;
						}
				}else
				{
					$this->errmsg = '用户注册绑定卡密已经使用完';
					return false;
				}
			}else
			{
				$this->errmsg = '注册绑定产品没有导入卡密';
				return false;
			}
		}else
		{
			$this->errmsg = '用户注册绑定充值卡产品不存在';
			return false;			
		}
		}else
		{
			$this->errmsg = '存在用户';
			return false;
		}
	}
	/**
	 * 
	 *2011-12-30-下午04:33:55 by 460932465
	 * @return 注册后执行，...
	 */
	protected function _after_reg()
	{
		return true;
	}
	/**
	 * 
	 *2011-12-30-下午04:22:20 by 460932465
	 * @return 注册成功返回TRUE，失败返回FALSE,可取$this->errmsg显示具体错误信息
	 * @param 用户信息数组|Array $userinfo
	 */
	function reg($userinfo)
	{
		
		if(is_array($userinfo))
		{
			$this->userinfo = $userinfo; //设置注册的信息
			if($this->_befor_reg())
			{
				QDB::getConn()->startTrans();
					//注册实现
					try {
						$this->userbase = new Userbase($this->userinfo);
						//设置代理等级
						if($this->userinfo['parentuserid'] != 0)
						{
							$this->userbase->userlevel = $this->userbase->parent->userlevel+1;
						}
						$this->userbase->save(0,'create');
						QDB::getConn()->completeTrans(true);
						return true;
					} catch (Exception $e) {
						$this->errmsg = $e->getmessage();
						QDB::getConn()->completeTrans(false);
						return false;
					}
					return $this->_after_reg(); //注册后	
			}else
			{
				return false;
			}
		}else
		{
			$this->errmsg = '传入参数必须是userbase数组';
			return false;
		}
	}
}
?>